/* ServerApp.java 
* Created on Dec 6, 2012 
*@Author: Mr.Viet 
*@Phone: 01683430426 
*@Email: phamvanvietcdhd@gmail.com
*/ 
package com.chat; 

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ServerApp extends JFrame implements ActionListener{
	
	private JLabel lblMessage; 
	private JButton btnStrat , btnStop;
	private ServerSocket server;
	private ClientAcept ca;
	public static Vector<ClientServerTheard> onlineUsers = new Vector<ClientServerTheard>();
	public static final String TYPE_SEND_MESS = "send";
	public static final String TYPE_CLOSE_APP = "close";
	public static final String TYPE_LOGIN_APP = "login";
	private final int PORT = 1000;
	
	public void createForm(){
		this.lblMessage = new JLabel("Server Socket");
		this.lblMessage.setFont(new Font("Arial", Font.PLAIN, 30));
		this.btnStrat = new JButton("Strat");
		this.btnStop = new JButton("Stop");
		this.btnStop.setEnabled(false);
		
		JPanel p = new JPanel();
		p.setLayout( new FlowLayout(FlowLayout.RIGHT));
		p.add(this.btnStrat);
		p.add(this.btnStop);
		
		this.getContentPane().add(this.lblMessage);
		this.getContentPane().add(p,BorderLayout.SOUTH);
		//Dang Ki Su Kien
		this.btnStop.addActionListener(this);
		this.btnStrat.addActionListener(this);
	}
	public void showForm(){
		this.setTitle("Chat");
		this.setSize(300,140);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public ServerApp() {
	}

	public static void main(String[] args) {
		ServerApp app = new ServerApp();
		app.createForm();
		app.showForm();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.btnStrat){
			this.startServer();
		}
		if(e.getSource() == this.btnStop){
			this.stopServer();
		}
	}
	public void stopServer(){
		if(!this.server.isClosed()){
			try {
				this.server.close();
				this.ca.interrupt();
				this.lblMessage.setText("Server is stopped...");
				this.btnStop.setEnabled(false);
				this.btnStrat.setEnabled(true);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void startServer(){
		try {
			this.server = new ServerSocket();
//			Tao Thread Cho Phep Ket Noi Tu Phia Client
			ca = new ClientAcept(server);
			ca.start();
			this.lblMessage.setText("Server is running...");
			this.btnStop.setEnabled(true);
			this.btnStrat.setEnabled(false);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void senOnlineUsers(){
		String online = "";
		int i =0;
		for (ClientServerTheard  cst : ServerApp.onlineUsers) {
			online += cst.getNickName() +";";
		}
		String mess = "server##useronline##"+online;
		for (ClientServerTheard cst : ServerApp.onlineUsers) {
			cst.sendMess(mess);
		}
	}
}
